﻿using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using PasswordSafe.Xml;
using System.Windows.Forms;
using PasswordSafe.Classes;

namespace PasswordSafe.Templates
{
    public class CategoryTemplate : FluidTemplate
    {

        public CategoryTemplate()
            : base()
        {
        }

        public CategoryTemplate(bool showCounter)
        {
            ShowCounter = showCounter;
        }

        private FluidLabel label;
        private FluidButton deleteBtn;
        private FluidButton confirmDeleteBtn;
        private FluidLabel countLabel;


        protected override void InitControl()
        {
            base.InitControl();
            ShowCounter = true;
            ButtonStretch = 100;

            Font buttonFont = new Font(FontFamily.GenericSansSerif, 8f, FontStyle.Bold);

            this.Bounds = new Rectangle(0, 0, 240, 50);
            FluidLabel l = new FluidLabel("", 8, 0, 224, 50);
            l.LineAlignment = StringAlignment.Center;
            l.Anchor = AnchorAll;
            label = l;
            l.Font = new Font(FontFamily.GenericSansSerif, 11f, FontStyle.Bold);

            Controls.Add(l);

            deleteBtn = new FluidButton("-", 4, 7, 20, 20);
            deleteBtn.ButtonSize = new Size(20, 20);
            deleteBtn.Shape = ButtonShape.Ellipse;
            deleteBtn.BackColor = Color.DarkRed; // Salmo, IndianRed Tomato
            deleteBtn.ForeColor = Color.White;
            deleteBtn.Font = buttonFont;
            deleteBtn.Click += new EventHandler(deleteBtn_Click);
            deleteBtn.Anchor = AnchorTL;
            deleteBtn.Visible = false;
            Controls.Add(deleteBtn);

            if (ShowCounter)
            {
                countLabel = new FluidLabel("", 240 - 40, 4, 36, 50 - 8);
                countLabel.Alignment = StringAlignment.Far;
                countLabel.LineAlignment = StringAlignment.Center;
                countLabel.ForeColor = Theme.Current.ListSecondaryForeColor;
                countLabel.Font = new Font(FontFamily.GenericSansSerif, 7f, FontStyle.Regular);
                countLabel.Anchor = AnchorRTB;
                Controls.Add(countLabel);
            }

            confirmDeleteBtn = new FluidButton("Delete", 240 - 50 - 4, 4, 50, 50 - 8);
            confirmDeleteBtn.ButtonSize = new Size(50, 24);
            confirmDeleteBtn.BackColor = Color.Red;
            confirmDeleteBtn.ForeColor = Color.White;
            confirmDeleteBtn.Font = buttonFont;
            confirmDeleteBtn.Anchor = AnchorRTB;
            confirmDeleteBtn.Click += new EventHandler(confirmDeleteBtn_Click);
            confirmDeleteBtn.Visible = false;
            Controls.Add(confirmDeleteBtn);


        }

        CategoriesListBox ListBox { get { return Parent as CategoriesListBox; } }

        int deleteIndex = -1;

        public int DeleteIndex
        {
            get { return deleteIndex; }
            set
            {
                if (deleteIndex != value)
                {
                    if (deleteIndex >= 0) ListBox.Invalidate(ListBox.GetItemBounds(deleteIndex));
                    deleteIndex = value;
                    if (value >= 0) ListBox.Invalidate(ListBox.GetItemBounds(deleteIndex));
                    ListBox.Invalidate();
                }
            }
        }

        void confirmDeleteBtn_Click(object sender, EventArgs e)
        {
            int deleteIndex = DeleteIndex;
            DeleteIndex = -1;
            ListBox.Categories.RemoveAt(deleteIndex);
        }

        public bool ShowCounter { get; set; }


        void deleteBtn_Click(object sender, EventArgs e)
        {
            int index = this.ItemIndex;
            ListBox.SelectedItemIndex = index;
            DeleteIndex = index;
        }

        protected override void OnBindValue(object value)
        {
            PasswordCategory c = value as PasswordCategory;
            if (c != null)
            {
                LayoutTemplate(c);
                label.Text = c.Title;
                
                if (ShowCounter)
                {
                    label.ForeColor = c.PasswordCount == 0 ? Theme.Current.GrayTextColor : Color.Empty;
                    countLabel.Text = c.PasswordCount.ToString();
                    confirmDeleteBtn.Visible = deleteIndex >= 0 && ItemIndex == deleteIndex;
                }
            }
            else
            {
                label.Text = "";
            }
        }

        public CategoriesListBox CategoryListBox { get { return Parent as CategoriesListBox; } }

        protected override void OnItemUpdate(object value)
        {
            base.OnItemUpdate(value);
            if (ShowCounter)
            {
                bool isSelected = this.CategoryListBox.SelectedItemIndex == this.ItemIndex;
                countLabel.ForeColor = isSelected ? Theme.Current.ListSecondarySelectedForeColor : Theme.Current.ListSecondaryForeColor;
            }
        }

        private void LayoutTemplate(PasswordCategory c)
        {
            if (c != null) deleteBtn.Visible = editMode && !c.ReadOnly;
        }

        private bool editMode;
        public bool EditMode
        {
            get { return editMode; }
            set
            {
                if (editMode != value)
                {
                    editMode = value;
                    OnEditModeChanged();
                }
            }
        }

        private int buttonStretch = 100;
        public int ButtonStretch
        {
            get { return buttonStretch; }
            set
            {
                if (buttonStretch != value)
                {
                    buttonStretch = value;
                    LayoutTemplate();
                }
            }
        }


        private void OnEditModeChanged()
        {
            if (!editMode) DeleteIndex = -1;
            LayoutTemplate();
        }

        private void LayoutTemplate()
        {
            BeginInit();
            deleteBtn.Visible = editMode;
            int w = Width;
            int h = Height;
            int distance = ScaleX(8);
            if (editMode) deleteBtn.Width = buttonStretch * ScaleX(20) / 100;
            int l = (editMode ? deleteBtn.Width : 0) + distance;
            label.Bounds = new Rectangle(l, 0, w - l, h);
            EndInit();
        }

    }
}
